from flask import Blueprint,request,make_response,jsonify,redirect
from flask_restful import Resource,Api
from utils.captcha.captcha import captcha
from utils.Myredis import r
import json
from utils.comm import send_message
import random
from application.models.users.user import *
from application.models.users.user import db
import time
from utils.Myjwt import myjwt
import functools,time
from db import db
rbac1_blue=Blueprint("rbac1",__name__)
api=Api(rbac1_blue)

# 添加用户
@rbac1_blue.route("/adduser",methods=["POST"])
def adduser():
    mobile=request.json.get("mobile")
    name=request.json.get("name")
    user=User(mobile=mobile,name=name)
    db.session.add(user)
    db.session.commit()
    return jsonify({"code":201,"msg":"添加成功"})
@rbac1_blue.route("/adduser",methods=["POST"])
def addusers():
    data=json.loads(request.data)
    name=data["name"]
    roleid=int(data["roleid"])
    sql="insert into user_table(name,roleid) values('%s',%d)"%(name,roleid)
    db.session.add(sql)
    db.session.commit()
    return jsonify({"code":200,"msg":"ok"})
# 添加角色
@rbac1_blue.route("/addrole",methods=["POST"])
def addrole():
    data=json.loads(request.data)
    pid=data["pid"]
    if data and data["name"]:
        # try:
            sql="insert into roles(name,pid) values ('%s',%d)"%(data["name"],int(pid))
            db.add(sql)
            db.commit()
            return jsonify({"code":200,"msg":"成功"})
        # except:
        #     return jsonify({"code":401,"msg":"添加失败"})
# 获取角色
@rbac1_blue.route("/getroles")
def getroles():
    sql="select * from roles"
    res=db.findAll(sql)
    return jsonify({"code":200,"msg":res})
# 基础角色
@rbac1_blue.route("/onerole")
def onerole():
    sql="select * from roles where pid==0"
    res=db.findAll(sql)
    return jsonify({"code":200,"msg":res})
# 添加资源
@rbac1_blue.route("/addresource",methods=["POST"])
def addresource():
    data=json.loads(request.data)
    name=data["name"]
    url=data["url"]
    pid=data["pid"]
    # try:
    sql="insert into resource(name,url,pid) values ('%s','%s',%d)"%(name,url,int(pid))
    db.add(sql)
    db.commit()
    return jsonify({"code":200,"msg":"添加成功"})
    # except:
    #     return jsonify({"code": 401, "msg": "添加失败"})
# 获取资源
@rbac1_blue.route("/getresource")
def getresource():
    sql="select * from resource"
    res=db.findAll(sql)
    lst=[{"key":i["id"],"label":i["name"]} for i in res]
    return jsonify({"code":200,"list":lst})
@rbac1_blue.route("/toplist")
def toplist():
    sql="select * from resource where pid=0"
    res=db.findAll(sql)
    return jsonify({"code":200,"msg":res})
# 给角色添加资源
@rbac1_blue.route("/setresou",methods=["POST"])
def setresou():
    data=json.loads(request.data)
    roleid=int(data["roleid"])
    reslist=data["reslist"]
    # 先删除对应的资源
    dsql="delete from roles_resource where rolesid=%d"%(roleid)
    db.add(dsql)
    db.commit()
    # 一一添加
    for i in reslist:
        asql="insert into roles_resource(rolesid, resourceid) values (%d,%d)"%(roleid,int(i))
        db.add(asql)
        db.commit()
    return jsonify({"code":200,"msg":"ok"})
# 获取角色对应的资源/
@rbac1_blue.route("/getresou")
def getresou():
    # 获取角色id
    id=int(request.args.get("id"))
    # 获取角色对应的资源
    sql="select resourceid from roles_resource where rolesid=%d"%(id)
    res=db.findAll(sql)
    lst=[i["resourceid"] for i in res]
    # 重组数据后返回
    return jsonify({"code":200,"lst":lst})
@rbac1_blue.route("/setrr")
def setrr():
    data=json.loads(request.data)
    roleid=data["roleid"]
    parentRole=data["parentRole"]
    reslist=data["reslist"]
    # 删除此角色对应的所有资源
    sql="delete from roles_resource where rolesid=%d"%(int(roleid))
    db.session.add(sql)
    rr=[]
    # 判断继承角色是否大于0 如果大于 查询继承角色
    if parentRole and int(parentRole)>0:
        sql1="select resourceid from roles_resource where rolesid=%d"%(int(parentRole))
        rr=db.findAll(sql1)
        # 查询当前角色继承的角色/
        sql2="update roles set pid=%d where id=%d"%(int(parentRole),int(roleid))
        db.session.add(sql2)
        # 获取配置资源reslist 并去重
        lst=[]
        for i in rr:
            lst.append(i["resourceid"])
        resourcelist=set(lst+reslist)
        # 更新角色资源表
        for i in resourcelist:
            sql3="insert into roles_resource values(%d,%d)"%(int(roleid),int(i))
            db.session.add(sql3)
        db.session.commit()
        return jsonify({"code":200})